package com.qianfeng.smartdevice.mapper;
//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.qianfeng.smartdevice.dto.UserDto;
import com.qianfeng.smartdevice.pojo.User;
import com.qianfeng.smartdevice.sql.provider.UserSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * Created by jackiechan on 2020/12/7 20:22
 *
 * @Author jackiechan
 */
public interface UserMapper {
    @Insert("INSERT INTO users(username,password,type) VALUES(#{username},#{password},#{type})")
    int addUser(UserDto userDto);

    //根据 用户名查询 id 是否存在,主要判断用户有没有注册
    @Select("SELECT id FROM users WHERE username = #{username}")
    Long getUserIdByUserName(String username);

    @Select("SELECT id,username,password,lastlogin,currentlogin,type,data1,data2,status,regdate FROM users WHERE id = #{id}")
    User getUserByUId(Long id);

    @Select("SELECT id,username,password,lastlogin,currentlogin,type,data1,data2,status,regdate FROM users WHERE username = #{username}")
    User getUserByUsername(String username);

    //@Select("SELECT id,username,password,lastlogin,currentlogin,type,data1,data2,status,regdate FROM users")
//    @Select("<script>\n" +
//            "\n" +
//            "            SELECT id,username,password,lastlogin,currentlogin,type,data1,data2,status,regdate\n" +
//            "            FROM users\n" +
//            "            <where>\n" +
//            "\n" +
//            "            <if test=\"status!=null and status != '-100'\">\n" +
//            "                 status =#{status}\n" +
//            "            </if>\n" +
//            "            </where>\n" +
//            "\n" +
//            "        </script>")
//    List<User> findAllUsers(int status);

    @Update("UPDATE users SET lastlogin=#{date} , currentlogin=#{date} WHERE id = #{id}")
    int updateUserLoginTime(@Param("id") Long id, @Param("date") Date date);

    @Update("UPDATE users SET lastlogin=currentlogin , currentlogin=#{date} WHERE username = #{username}")
    int updateUserLoginTimeByUserName(@Param("username") String username, @Param("date") Date date);

    @SelectProvider(value = UserSqlProvider.class,method = "getUpdateSql")
    void updateUser(UserDto userDto);

    @Update("UPDATE users SET status='0' WHERE id =#{id}")
    void deleteUser(Long id);

    @Update({"<script>",
            "update users set status='0' where id in ",
            "<foreach collection=\"ids\" item=\"userId\" index=\"index\" open=\"(\" separator=\",\" close=\")\">",
            "#{userId}",
            "</foreach>",
            "</script>"})
    void deleteUsers(@Param("ids") String[] ids);

    @Select(" <script>\n" +
            "\n" +
            "            SELECT id,username,password,lastlogin,currentlogin,type,data1,data2,status,regdate\n" +
            "            FROM users\n" +
            "            <where>\n" +
            "                <if test=\"username!=null and username != ''\">\n" +
            "                    AND  username like concat('%',#{username},'%')\n" +
            "                </if>\n" +
            "            <if test=\"status!=null and status != '-100'\">\n" +
            "               AND  status =#{status}\n" +
            "            </if>\n" +
            "            </where>\n" +
            "\n" +
            "        </script>")
    List<User> findAllUsers(@Param("username") String usernamelike, @Param("status") int status);


}
